home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet internetowy / Przegladarki internetowe / Mozilla Seamonkey 1.0.5 pl / seamonkey-1.0.5.pl-PL.win32.installer.exe / VENKMAN.XPI / bin / chrome / venkman.jar / content / venkman / venkman-munger.js < prev    next >
Encoding:
JavaScript  |  2004-04-18  |  5.5 KB  |  170 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is The JavaScript Debugger.
  17.  *
  18.  * The Initial Developer of the Original Code is
  19.  * Netscape Communications Corporation.
  20.  * Portions created by the Initial Developer are Copyright (C) 1998
  21.  * the Initial Developer. All Rights Reserved.
  22.  *
  23.  * Contributor(s):
  24.  *   Robert Ginda, <rginda@netscape.com>, original author
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. function CMungerEntry (name, regex, className, tagName)
  41. {
  42.     
  43.     this.name = name;
  44.     this.tagName = (tagName) ? tagName : "html:span";
  45.  
  46.     if (regex instanceof RegExp)
  47.         this.regex = regex;
  48.     else
  49.         this.lambdaMatch = regex;
  50.     
  51.     if (typeof className == "function")
  52.         this.lambdaReplace = className;
  53.     else 
  54.         this.className = className;
  55.     
  56. }
  57.  
  58. function CMunger () 
  59. {
  60.     
  61.     this.entries = new Object();
  62.     
  63. }
  64.  
  65. CMunger.prototype.enabled = true;
  66.  
  67. CMunger.prototype.addRule =
  68. function mng_addrule (name, regex, className)
  69. {
  70.     
  71.     this.entries[name] = new CMungerEntry (name, regex, className);
  72.     
  73. }
  74.  
  75. CMunger.prototype.delRule =
  76. function mng_delrule (name)
  77. {
  78.  
  79.     delete this.entries[name];
  80.     
  81. }
  82.  
  83. CMunger.prototype.munge =
  84. function mng_munge (text, containerTag, data)
  85. {
  86.     var entry;
  87.     var ary;    
  88.  
  89.     if (!text) //(ASSERT(text, "no text to munge"))
  90.         return "";
  91.      
  92.     if (typeof text != "string")
  93.         text = String(text);
  94.  
  95.     if (!containerTag)
  96.     {
  97.         containerTag =
  98.             document.createElementNS (NS_XHTML, this.tagName);
  99.     }
  100.  
  101.     if (this.enabled)
  102.     {
  103.         for (entry in this.entries)
  104.         {
  105.             if (typeof this.entries[entry].lambdaMatch == "function")
  106.             {
  107.                 var rval;
  108.                 
  109.                 rval = this.entries[entry].lambdaMatch(text, containerTag,
  110.                                                        data,
  111.                                                        this.entries[entry]);
  112.                 if (rval)
  113.                     ary = [(void 0), rval];
  114.                 else
  115.                     ary = null;
  116.             }
  117.             else
  118.                 ary = text.match(this.entries[entry].regex);
  119.             
  120.             if ((ary != null) && (ary[1]))
  121.             {
  122.                 var startPos = text.indexOf(ary[1]);
  123.                 
  124.                 if (typeof this.entries[entry].lambdaReplace == "function")
  125.                 {
  126.                     this.munge (text.substr(0,startPos), containerTag,
  127.                                 data);
  128.                     this.entries[entry].lambdaReplace (ary[1], containerTag,
  129.                                                        data,
  130.                                                        this.entries[entry]);
  131.                     this.munge (text.substr (startPos + ary[1].length,
  132.                                              text.length), containerTag,
  133.                                 data);
  134.                 
  135.                     return containerTag;
  136.                 }
  137.                 else
  138.                 {
  139.                     this.munge (text.substr(0,startPos), containerTag,
  140.                                 data);
  141.                     
  142.                     var subTag = 
  143.                         document.createElementNS (NS_XHTML,
  144.                                                   this.entries[entry].tagName);
  145.  
  146.                     subTag.setAttribute ("class",
  147.                                          this.entries[entry].className);
  148.                     var wordParts = splitLongWord (ary[1],
  149.                                                    client.MAX_WORD_DISPLAY);
  150.                     for (var i in wordParts)
  151.                     {
  152.                         subTag.appendChild (document.createTextNode (wordParts[i]));
  153.                         subTag.appendChild (htmlWBR());
  154.                     }
  155.                     
  156.                     containerTag.appendChild (subTag);
  157.                     this.munge (text.substr (startPos + ary[1].length,
  158.                                              text.length), containerTag, data);
  159.  
  160.                     return containerTag;
  161.                 }
  162.             }
  163.         }
  164.     }
  165.  
  166.     containerTag.appendChild (document.createTextNode (text));
  167.     return containerTag;
  168.     
  169. }
  170.